package com.bankcomm.voice;

import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import com.rytong.tools.utils.Utils;

/**
 * 声音管理器
 * @author liujia
 * @email liujia9234@126.com
 */
public class SoundManager {
	private static final String TAG = "SoundManager";
	static private SoundManager _instance;
	private static SoundPool mSoundPool;
	private static Map<Integer, Integer> mSoundPoolMap;
	private static AudioManager  mAudioManager;
	private static Context mContext;
 
	private SoundManager(Context theContext)
	{
		initSounds(theContext);
		loadSounds();
	}
 
	/**
	 * 单例模式，如果SoundManager实例不存在就创建，反之则直接返回已经
         *  存在的SoundManager实例
	 *
	 * @return Returns the single instance of the SoundManager
	 */
	static synchronized public SoundManager getInstance(Context theContext)
	{
	    if (_instance == null){
	      _instance = new SoundManager(theContext);
	    }
	    return _instance;
	 }
	
	public boolean isPlayer(){
		return mAudioManager.isMusicActive();
	}
 
	/**
	 * Initialises the storage for the sounds
	 * @param theContext The Application context
	 */
	public  void initSounds(Context theContext)
	{
		 mContext = theContext;
	     mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
	     mSoundPoolMap = new HashMap<Integer, Integer>();
	     mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
	}
 
	/**
	 * Add a new Sound to the SoundPool
	 *
	 * @param Index - The Sound Index for Retrieval
	 * @param SoundID - The Android ID for the Sound asset.
	 */
	public void addSound(int Index,int SoundID)
	{
		mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
	}
 
	/**
	 * Loads the various sound assets
	 * Currently hardcoded but could easily be changed to be flexible.
	 */
	public void loadSounds()
	{
//		mSoundPoolMap.put(1, mSoundPool.load(mContext, R.drawable.bt_end1, 1));
//		mSoundPoolMap.put(2, mSoundPool.load(mContext, R.drawable.bt_end0, 1));
//		mSoundPoolMap.put(3, mSoundPool.load(mContext, R.drawable.button_sound, 1));
	}
 
	/**
	 * Plays a Sound
	 *
	 * @param index - The Index of the Sound to be played
	 * @param speed - The Speed to play not, not currently used but included for compatibility
	 */
	public void playSound(int index,float speed)
	{
		Utils.LogD(TAG, "准备播放声音文件index："+index+"");
		long beginDate = System.currentTimeMillis();
	    float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
	    streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
	    mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 0, 0, speed);
	    long endDate = System.currentTimeMillis();
	    Utils.LogD(TAG, "播放完成,总共播放了"+(endDate-beginDate)+"毫秒");
	}
	
	
 
	/**
	 * Stop a Sound
	 * @param index - index of the sound to be stopped
	 */
	public static void stopSound(int index)
	{
		mSoundPool.stop(mSoundPoolMap.get(index));
	}
 
	/**
	 * Deallocates the resources and Instance of SoundManager
	 */
	public static void cleanup()
	{
		mSoundPool.release();
		mSoundPool = null;
	    mSoundPoolMap.clear();
	    mAudioManager.unloadSoundEffects();
	    _instance = null;
 
	}

	
}
